package com.ufgov.agcfs.baseData.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import uap.iweb.entity.DataTable;
import uap.iweb.exception.WebRuntimeException;
import uap.iweb.icontext.IWebViewContext;

import com.ufgov.agcfs.baseData.entity.AssetType;
import com.ufgov.agcfs.baseData.entity.UpdateType;
import com.ufgov.agcfs.baseData.service.AssetTypeService;
import com.ufgov.ip.base.service.IPDataTableService;

@Controller("baseData.assetType.assetTypeController")
@Scope("prototype")
@RequestMapping(value = "/baseData/assetType")
public class AssetTypeController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private AssetTypeService assetTypeService;	
	@Autowired 
	IPDataTableService iPDataTableService;
	
	DataTable<AssetType> assetTypeDataTable;
	
	public void loadAssetType() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = iPDataTableService.createSearchParamsStartingWith("search_",assetTypeDataTable);
			Sort sort = iPDataTableService.buildSortRequest(Direction.ASC, "nd");

			List<AssetType> categoryPage=assetTypeService.findSearch(searchParams, sort);
			assetTypeDataTable.remove(assetTypeDataTable.getAllRow());
			assetTypeDataTable.set(categoryPage.toArray(new AssetType[0]));
			IWebViewContext.getResponse().write("true");
		} catch (Exception e) {
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}
	}
	
	@RequestMapping(method = RequestMethod.GET,value="getAssetList")
	public  @ResponseBody HashMap<String,Object> getAssetList(@RequestParam(value="start")int start,
			   @RequestParam(value="length")int length,
			   @RequestParam(value="draw")int draw,HttpServletRequest request){
		try {
			List<AssetType> assetList = assetTypeService.getAssetList();
			int assetCnt = assetList.size();
			HashMap<String,Object> hashRet = new HashMap<String, Object>();
			hashRet.put("draw", draw);
			hashRet.put("recordsTotal",assetCnt);
			hashRet.put("recordsFiltered",assetCnt);
			hashRet.put("data",assetList);
			return hashRet;
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}
	
	@RequestMapping(method = RequestMethod.GET,value="getAllNd")
	public @ResponseBody List<String> getAllnd(HttpServletRequest request, HttpServletResponse response){
		List<String> ndList = new ArrayList<String>();
		try {
			ndList = assetTypeService.getAllNd();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ndList;		
	}	
	
	
	@RequestMapping(method = RequestMethod.POST,value="saveAssetType")
	public  @ResponseBody Map<String, String> saveAssetType(@RequestBody AssetType assetType,HttpServletRequest request, HttpServletResponse response){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "保存成功！");
		try {
			assetTypeService.saveAssetType(assetType,resultMap);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			resultMap.put("result", "fail");
			resultMap.put("reason", "服务端繁忙，请稍后重试！");
		}
		return resultMap;
	}
	
	
	
	@RequestMapping(method = RequestMethod.POST,value="delAssetType")
	public @ResponseBody Map<String, String> delAssetType(HttpServletRequest request, HttpServletResponse response){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "删除成功！");
		try {
			String id = request.getParameter("id");
			assetTypeService.delAssetType(id,resultMap);
		} catch (Exception e) {
			// TODO 自动生成的 catch块
			resultMap.put("result", "fail");
			resultMap.put("reason", "服务端繁忙，请稍后重试！");
		}
		return resultMap;
		
	}	
	
}
